package com.example.mystore.service;

import com.example.mystore.model.ProductReview;
import com.example.mystore.model.User;
import com.example.mystore.repository.ReviewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewService {

    @Autowired
    private ReviewRepository reviewRepository;

    // 获取商品评论列表
    public List<ProductReview> getProductReviews(Long productId) {
        return reviewRepository.findByProductId(productId);
    }

    // 提交评论
    public void submitReview(Long userId, ProductReview review) {
        review.setUserId(userId);
        reviewRepository.save(review);
    }

    // 点赞评论
    public void likeReview(Long userId, Long reviewId) {
        ProductReview review = reviewRepository.findById(reviewId).orElse(null);
        if (review != null) {
            review.addLike(userId);
            reviewRepository.save(review);
        }
    }
}